package com.ai.libface

import android.content.Context
import android.util.Log
import com.tenginekit.AndroidConfig
import com.tenginekit.KitCore
import com.tenginekit.face.Face
import com.tenginekit.face.FaceDetectInfo
import com.tenginekit.face.FaceLandmarkInfo
import com.tenginekit.jni.FaceSchedule
import java.util.*


object ObjLiveFace {
    var BUFFERLEN:Int = 64
    val action: Int = 0
    var landmarks= arrayOfNulls<FaceLandmarkInfo>(BUFFERLEN)
    var cur=0
    var H=15 //闭眼睛l=15 张开l=30

//    private var detectInfos: List<FaceDetectInfo>? = null
//    var detectInfos = arrayOfNulls<FaceLandmarkInfo>(BUFFERLEN)




    fun checkAction():Int{
        var maxPitch:Float = -90.0f
        var maxYaw:Float=-90f
        var minPitch:Float=90f
        var minYaw:Float=90f
        var minE:Float=999f
        var maxE:Float=0f
        for (m in landmarks){
            if (m==null)continue
            if (m.pitch>maxPitch)maxPitch=m.pitch
            if (m.pitch<minPitch)minPitch=m.pitch
            if (m.yaw>maxYaw)maxYaw=m.yaw
            if (m.yaw<minYaw)minYaw=m.yaw
            //left: 122,130 right:105,115
            var p122=m.landmarks[121]
            var p130=m.landmarks[129]
            var p105=m.landmarks[105]
            var p115=m.landmarks[113]

            Log.i("MYDEBUG", "p130:${p130.Y},p105:${p105.Y},p115:${p115.Y}")

            var l= p130.Y-p122.Y //(p130.X-p122.X)*(p130.X-p122.X)+(p130.Y-p122.Y)*(p130.Y-p122.Y)
            var r= p115.Y-p105.Y //(p105.X-p115.X)*(p105.X-p115.X)+(p105.Y-p115.Y)*(p105.Y-p115.Y)
            if (l<minE)minE=l
            if (r<minE)minE=r
            //if (l>maxE)maxE=l
            //if (r>maxE)maxE=r
        }
        Log.i("MYDEBUG", "pitch:${maxPitch - minPitch},yaw:${maxYaw - minYaw},eye:${minE}")
        if (maxPitch-minPitch>15)
            return 1//点头
        if (maxYaw-minYaw>10)
            return 2//摇头
        if (minE<H) return 3 //闭眼睛l=15 张开l=30

        else return 0
    }

    fun processImage(mNV21Bytes: ByteArray?): Int {
        /**
         * 获取人脸信息
         */
        val faceDetect = Face.detect(mNV21Bytes)
        var landmarkInfos: List<FaceLandmarkInfo> = ArrayList()
        if (faceDetect.faceCount > 0) {
            landmarks[cur]=faceDetect.landmark2d()[0]

//            var pre=cur-1
//            if (pre<0)pre=BUFFERLEN-1
//            var lcur= landmarks.get(cur)
//            var lpre= landmarks.get(pre)
//            Log.i("MYDEBUG","pitch:${lcur!!.pitch},yaw:${lcur.yaw},roll:${lcur.roll}")
            cur++
            if (cur>=16)cur=0

//            if( lpre==null)return 0
//
//            val c168: TenginekitPoint = lcur!!.landmarks!!.get(167)
//            val c178: TenginekitPoint = lcur!!.landmarks!!.get(177)
//            val c180: TenginekitPoint = lcur!!.landmarks!!.get(179)
//            val p168: TenginekitPoint = lpre!!.landmarks!!.get(167)
//            val p178: TenginekitPoint = lpre!!.landmarks!!.get(177)
//            val p180: TenginekitPoint = lpre!!.landmarks!!.get(179)
//
////        #nose 对齐
////                pos=cnose-lnose
////        x=abs(cur[2]-pos[0]-la[2])
////        y=abs(cur[7]-pos[1]-la[7])
////        if x>5 or y>5: #幅度要大于5
////        if x<y:
////        print("点头 ",x,y)
////        else:
////        print("摇头 ",x,y)
//            var px = c180.X-p180.X
//            var py = c180.Y-p180.Y
//            var x=abs(c180.X-px-p180.X)
//            var y=abs(c180.Y-py-p180.Y)
//            if (x>5 || y>5){
//                if (x>y)
//                    return 1;//"点头 "
//                else
//                    return 2;//摇头
//            }
        }
        return 0//
    }

    fun setRotation(ori: Int, isScreenRotate: Boolean, outputW: Int, outputH: Int) {
        if (!KitCore.getInit()) return
        FaceSchedule.setOri(ori, isScreenRotate, outputW, outputH)
    }

    /**
     * Switch cameras
     */
    fun switchCamera(back: Boolean) {
        if (!KitCore.getInit()) return
        if (back) FaceSchedule.switchCamera(1) else FaceSchedule.switchCamera(0)
    }
    /**
     * 初始化
     * */
    fun init(context: Context, previewWidth: Int, previewHeight: Int, ScreenWidth: Int, ScreenHeight: Int){
        /**
         * 初始化
         */
        KitCore.init(
                context,
                AndroidConfig
                        .create()
                        .setCameraMode()
                        .openFunc(AndroidConfig.Func.Detect).openFunc(AndroidConfig.Func.Landmark)
                        .setDefaultInputImageFormat()
                        .setInputImageSize(previewWidth, previewHeight)
                        .setOutputImageSize(ScreenWidth as Int, ScreenHeight as Int)
        )

    }

    fun release() {
        /**
         * 释放
         */
        KitCore.release()
    }
}